#define _CRT_SECURE_NO_WARNINGS
#include"heap.h"
//int main() {
//	HP hp;
//	HeapInit(&hp);
//	int a[] = { 1,5,6,8,2,12,89,45,78,46 };
//	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
//		HeapPush(&hp, a[i]);
//	}
//	HeapPrint(&hp);
//	int k = 5;
//	while (k--) {
//		printf("%d ", HeapTop(&hp));
//		HeapPop(&hp);
//	}
//	return 0;
//}

//
//void my_heap_sort(int* a, int n)
//{
//	/*for (int i = 1; i < n; i++) {
//		AdjustUp(a, i);}*/
//
//	for (int i = (n - 1 - 1) / 2; i >= 0; i--) {
//		AdjustDown(a, n, i);
//	}
//
//	
//	for (int i = n - 1; i > 0; i--) {
//		Swap(&a[i], &a[0]);
//		AdjustDown(a, i, 0);
//	}
//}
//int main() {
//	int a[] = { 45,12,7,9,13,62,96,17,100,46,23,85 };
//	my_heap_sort(a, sizeof(a) / sizeof(a[0]));
//	for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
//		printf("%d ", a[i]);
//	}
//	return 0;
//}

void CreateNData() {
	int n = 1000000;
	srand((unsigned int)time(NULL));

	FILE* fin = fopen("data.txt", "w");
	if (fin == NULL) {
		perror("fopen error");
		return;
	}
	for (int i = 0; i < n; i++) {
		int x = (int)(((double)rand() / RAND_MAX) * 1000000);
		fprintf(fin,"%d\n", x);

	}
	fclose(fin);
	fin = NULL;
}

void PrintTopK(const char* filename, int k) {
	FILE* fout = fopen(filename, "r");
	if (fout == NULL) {
		perror("fopen fail");
		return;
	}
	int* minheap = (int*)malloc(sizeof(int) * k);
	if (minheap == NULL) {
		perror("malloc fail");
		return;
	}
	for (int i = 0; i < k; i++) {
		fscanf(fout,"%d", &minheap[i]);
	}
	for (int i = (k - 2) / 2; i >= 0; i--) {
		AdjustDown(minheap, k, i);
	}
	int x = 0;
	while (fscanf(fout, "%d", &x) != EOF) {
		if (x > minheap[0]) {
			minheap[0] = x;
			AdjustDown(minheap, k, 0);
		}
	}
	for (int i = 0; i < k; i++) {
		printf("%d ", minheap[i]);
	}
	printf("\n");
	free(minheap);
	fclose(fout);
}
int main() {

	//CreateNData();
	PrintTopK("data.txt", 10);
	return 0;
}